<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>用 TrueType 字体向图像写入文本</title>

 </head>
 <body><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.imagettfbbox.html">imagettfbbox</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.imagetypes.html">imagetypes</a></div>
 <div class="up"><a href="ref.image.html">GD and Image 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div><hr /><div id="function.imagettftext" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">imagettftext</h1>
  <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">imagettftext</span> &mdash; <span class="dc-title">用 TrueType 字体向图像写入文本</span></p>

 </div>
 <div class="refsect1 description" id="refsect1-function.imagettftext-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="type">array</span> <span class="methodname"><strong>imagettftext</strong></span>
     ( <span class="methodparam"><span class="type">resource</span> <code class="parameter">$image</code></span>
    , <span class="methodparam"><span class="type">float</span> <code class="parameter">$size</code></span>
    , <span class="methodparam"><span class="type">float</span> <code class="parameter">$angle</code></span>
    , <span class="methodparam"><span class="type">int</span> <code class="parameter">$x</code></span>
    , <span class="methodparam"><span class="type">int</span> <code class="parameter">$y</code></span>
    , <span class="methodparam"><span class="type">int</span> <code class="parameter">$color</code></span>
    , <span class="methodparam"><span class="type">string</span> <code class="parameter">$fontfile</code></span>
    , <span class="methodparam"><span class="type">string</span> <code class="parameter">$text</code></span>
   )</div>

  <p class="para rdfs-comment">
   使用 TrueType 字体将 指定的 <em><code class="parameter">text</code></em> 写入图像。
  </p>
 </div>

 <div class="refsect1 parameters" id="refsect1-function.imagettftext-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>

    <dt>
<em><code class="parameter">image</code></em></dt>

<dd>
<p class="para">由图象创建函数(例如<span class="function"><a href="function.imagecreatetruecolor.html" class="function">imagecreatetruecolor()</a></span>)返回的图象资源。</p></dd>

    
     <dt>
<em><code class="parameter">size</code></em></dt>

     <dd>

      <p class="para">字体的尺寸。根据 GD 的版本，为像素尺寸（GD1）或点（磅）尺寸（GD2）。</p>
     </dd>

    
    
     <dt>
<em><code class="parameter">angle</code></em></dt>

     <dd>

      <p class="para">
       角度制表示的角度，0 度为从左向右读的文本。更高数值表示逆时针旋转。例如
       90 度表示从下向上读的文本。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">x</code></em></dt>

     <dd>

      <p class="para">
       由 <em><code class="parameter">x</code></em>，<em><code class="parameter">y</code></em>
       所表示的坐标定义了第一个字符的基本点（大概是字符的左下角）。这和
       <span class="function"><a href="function.imagestring.html" class="function">imagestring()</a></span> 不同，其 <em><code class="parameter">x</code></em>，<em><code class="parameter">y</code></em> 定义了第一个字符的左上角。例如
       &quot;top left&quot; 为 0, 0。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">y</code></em></dt>

     <dd>

      <p class="para">
       Y 坐标。它设定了字体基线的位置，不是字符的最底端。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">color</code></em></dt>

     <dd>

      <p class="para">
       颜色索引。使用负的颜色索引值具有关闭防锯齿的效果。见
       <span class="function"><a href="function.imagecolorallocate.html" class="function">imagecolorallocate()</a></span>。
      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">fontfile</code></em></dt>

     <dd>

      <p class="para">
       是想要使用的 TrueType 字体的路径。
      </p>
      <p class="para">
       根据 PHP 所使用的 GD 库的不同，<em class="emphasis">当 <em><code class="parameter">fontfile</code></em>
       没有以 <em>/</em> 开头时则 <em>.ttf</em>
       将被加到文件名之后</em>并且会在库定义字体路径中尝试搜索该文件名。
      </p>
      <p class="para">
       当使用的 GD 库版本低于 2.0.18 时，一个空格字符
       而不是分号将被用来作为不同字体文件的“路径分隔符”。不小心使用了此特性将会导致一条警告信息：<em>Warning:
       Could not find/open font</em>。对受影响的版本来说唯一解决方案就是将字体移动到不包含空格的路径中去。
      </p>
      <p class="para">
       很多情况下字体都放在脚本的同一个目录下。下面的小技巧可以减轻包含的问题。
       <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;the&nbsp;enviroment&nbsp;variable&nbsp;for&nbsp;GD<br /></span><span style="color: #0000BB">putenv</span><span style="color: #007700">(</span><span style="color: #DD0000">'GDFONTPATH='&nbsp;</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">realpath</span><span style="color: #007700">(</span><span style="color: #DD0000">'.'</span><span style="color: #007700">));<br /><br /></span><span style="color: #FF8000">//&nbsp;Name&nbsp;the&nbsp;font&nbsp;to&nbsp;be&nbsp;used&nbsp;(note&nbsp;the&nbsp;lack&nbsp;of&nbsp;the&nbsp;.ttf&nbsp;extension)<br /></span><span style="color: #0000BB">$font&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'SomeFont'</span><span style="color: #007700">;<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
       </div>

      </p>
     </dd>

    
    
     <dt>
<em><code class="parameter">text</code></em></dt>

     <dd>

      <p class="para">
       UTF-8 编码的文本字符串。
      </p>
      <p class="para">
       可以包含十进制数字化字符表示（形式为：&amp;#8364;）来访问字体中超过位置
       127 的字符。UTF-8 编码的字符串可以直接传递。
      </p>
      <p class="para">
       命名实体，比如 &amp;copy; 是不支持的。可以考虑使用 <span class="function"><a href="function.html-entity-decode.html" class="function">html_entity_decode()</a></span>
       来解码命名实体为 UTF-8 字符。 （自 PHP 5.0.0 开始 html_entity_decode()
       开始支持）
      </p>
      <p class="para">
       如果字符串中使用的某个字符不被字体支持，一个空心矩形将替换该字符。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>

 <div class="refsect1 returnvalues" id="refsect1-function.imagettftext-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
   返回一个含有 8
   个单元的数组表示了文本外框的四个角，顺序为坐下角，右下角，右上角，左上角。这些点是相对于文本的而和角度无关，因此“左上角”指的是以水平方向看文字时其左上角。
  </p>
 </div>

 <div class="refsect1 changelog" id="refsect1-function.imagettftext-changelog">
  <h3 class="title">更新日志</h3>
  <p class="para">
   <table class="doctable informaltable">
    
     <thead>
      <tr>
       <th>版本</th>
       <th>说明</th>
      </tr>

     </thead>

     <tbody class="tbody">
      <tr>
       <td>5.2.0</td>
       <td>
        It is now possible to specify an hexadecimal entity in 
        <em><code class="parameter">text</code></em>.
       </td>
      </tr>

     </tbody>
    
   </table>

  </p>
 </div>

 <div class="refsect1 examples" id="refsect1-function.imagettftext-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3198">
    <p><strong>Example #1 <span class="function"><strong>imagettftext()</strong></span> 例子</strong></p>
    <div class="example-contents"><p>
     本例中的脚本将生成一个白色的 400x30 像素 PNG 图像，其中有黑色（带灰色阴影）Arial
     字体写的“Testing...”。
    </p></div>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /></span><span style="color: #FF8000">//&nbsp;Set&nbsp;the&nbsp;content-type<br /></span><span style="color: #0000BB">header</span><span style="color: #007700">(</span><span style="color: #DD0000">'Content-Type:&nbsp;image/png'</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;the&nbsp;image<br /></span><span style="color: #0000BB">$im&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecreatetruecolor</span><span style="color: #007700">(</span><span style="color: #0000BB">400</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">30</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Create&nbsp;some&nbsp;colors<br /></span><span style="color: #0000BB">$white&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">255</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$grey&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">128</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">128</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$black&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">imagecolorallocate</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagefilledrectangle</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">399</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">29</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$white</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;The&nbsp;text&nbsp;to&nbsp;draw<br /></span><span style="color: #0000BB">$text&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'Testing...'</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">//&nbsp;Replace&nbsp;path&nbsp;by&nbsp;your&nbsp;own&nbsp;font&nbsp;path<br /></span><span style="color: #0000BB">$font&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'arial.ttf'</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">//&nbsp;Add&nbsp;some&nbsp;shadow&nbsp;to&nbsp;the&nbsp;text<br /></span><span style="color: #0000BB">imagettftext</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">11</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">21</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$grey</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$font</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Add&nbsp;the&nbsp;text<br /></span><span style="color: #0000BB">imagettftext</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">0</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">10</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">20</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$black</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$font</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">$text</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">//&nbsp;Using&nbsp;imagepng()&nbsp;results&nbsp;in&nbsp;clearer&nbsp;text&nbsp;compared&nbsp;with&nbsp;imagejpeg()<br /></span><span style="color: #0000BB">imagepng</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">imagedestroy</span><span style="color: #007700">(</span><span style="color: #0000BB">$im</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

    <div class="example-contents"><p>以上例程的输出类似于：</p></div>
    <div class="mediaobject">
     
     <div class="imageobject">
      <img src="images/21009b70229598c6a80eef8b45bf282b-imagettftext.png" alt="例子输出：imagettftext()" width="400" height="30" />
     </div>
    </div>
   </div>
  </p>
 </div>

 <div class="refsect1 notes" id="refsect1-function.imagettftext-notes">
  <h3 class="title">注释</h3>
  <blockquote class="note"><p><strong class="note">Note</strong>: 
   <p class="para">
    本函数同时需要 GD 库和
    <a href="http://www.freetype.org/" class="link external">&raquo;&nbsp;FreeType</a> 库。.
   </p>
  </p></blockquote>
 </div>

 <div class="refsect1 seealso" id="refsect1-function.imagettftext-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.imagettfbbox.html" class="function" rel="rdfs-seeAlso">imagettfbbox()</a> - 取得使用 TrueType 字体的文本的范围</span></li>
   </ul>
  </p>
 </div>

</div><hr /><div class="manualnavbar" style="text-align: center;">
 <div class="prev" style="text-align: left; float: left;"><a href="function.imagettfbbox.html">imagettfbbox</a></div>
 <div class="next" style="text-align: right; float: right;"><a href="function.imagetypes.html">imagetypes</a></div>
 <div class="up"><a href="ref.image.html">GD and Image 函数</a></div>
 <div class="home"><a href="index.html">PHP Manual</a></div>
</div></body></html>
